//
// Created by 1 on 2020-10-12.
//
#include "Solution.h"
#include <stack>
using namespace std;
#ifndef CTY_REVERSEDLIST_H
#define CTY_REVERSEDLIST_H

struct ListNode{
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(nullptr) {
    }
};

class ReversedList:Solution<ListNode*> {
public:
    ReversedList() {}
    ~ReversedList() { }

    ListNode* anw(){
        return nullptr;
    }

//    ListNode* anw(ListNode *pHead) {
//        stack<ListNode *> sList;
//        while (pHead) {
//            sList.push(const_cast<ListNode *&&>(pHead));
//            pHead = pHead->next;
//        }
//        ListNode *nP = sList.top();
//        ListNode *newHead = nP;
//        ListNode *index = newHead;
//        sList.pop();
//        while (!sList.empty()) {
//            nP = sList.top();
//            nP->next = nullptr;
//            index->next = nP;
//            index = nP;
//            sList.pop();
//        }
//        return newHead;
//    }


    ListNode* anw(ListNode *pHead) {
        if(pHead == nullptr || pHead->next == nullptr)
            return pHead;
        ListNode* cacheHead = pHead;
        ListNode* cacheSecond = pHead->next;
        while(cacheSecond != nullptr){

            cacheHead->next = cacheSecond->next;
            cacheSecond->next = pHead;

            pHead = cacheSecond;
            cacheSecond = cacheHead->next;
        }

        return pHead;
    }

    void  selfTest(){
        ListNode* pHead = new ListNode(10);
        ListNode* p2 = new ListNode(20);
        ListNode* p3 = new ListNode(30);
        ListNode* p4 = new ListNode(40);
        pHead->next = p2;
        p2->next = p3;
        p3->next = p4;
        pHead = anw(pHead);
        delete p4;
        delete p3;
        delete p2;
        delete pHead;
    }
};


#endif //CTY_REVERSEDLIST_H
